package com.sd.document;

import com.jfinal.core.Controller;
import com.sd.document.parameters.*;
import javafx.scene.Camera;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 需求：获取xml数据信息、获取前端提交数据、写入前端数据
 */

public class XmlParse extends Controller {
    private Document document;
    //获取xml文件信息
    public void GetParameter() throws DocumentException {

        String path = "E:\\ready\\xml\\videoMonitor1.1.0.xml";
        SAXReader reader = new SAXReader();
        document = reader.read(path);

        //获取第1栏信息:设备设置
        DeviceConfig deviceConfig = new DeviceConfig();
        deviceConfig.setId(document.selectSingleNode("//videoMonitor/deviceConfig/id").getText());//获取设备id
        deviceConfig.setIdDesc(((Element)document.selectSingleNode("//videoMonitor/deviceConfig/id")).attributeValue("desc"));//获取设备id描述
        deviceConfig.setCameraNameWatermark(document.selectSingleNode("//videoMonitor/deviceConfig/cameraNameWatermark").getText());//是否叠加通道名称
        deviceConfig.setCameraNameWatermarkDesc(((Element)document.selectSingleNode("//videoMonitor/deviceConfig/cameraNameWatermark")).attributeValue("desc"));//获取描述
        deviceConfig.setDeviceInfoWatermark(document.selectSingleNode("//videoMonitor/deviceConfig/deviceInfoWatermark").getText());;//是否叠加设备信息，太阳能、电池状态
        deviceConfig.setDeviceInfoWatermarkDesc(((Element)document.selectSingleNode("//videoMonitor/deviceConfig/deviceInfoWatermark")).attributeValue("desc"));//获取描述

        //获取第2栏信息:摄像头设置
        CamerasConfig camerasConfig = new CamerasConfig();
        CamerasConfig.Camera camera = new CamerasConfig.Camera();
        camerasConfig.setCamerasConfigDesc(((Element)document.selectSingleNode("//videoMonitor/camerasConfig")).attributeValue("desc"));//获取摄像头设置描述
        camerasConfig.setCamerasConfigDesc(((Element)document.selectSingleNode("//videoMonitor/camerasConfig/camera")).attributeValue("desc"));//获取摄像头描述

        camera.setName(document.selectSingleNode("//videoMonitor/camerasConfig/camera/name").getText());//摄像头名称
        camera.setNameDesc(((Element)document.selectSingleNode("//videoMonitor/camerasConfig/camera/name")).attributeValue("desc"));//获取摄像头名称描述
        camera.setVidioCodingFormat(document.selectSingleNode("//videoMonitor/camerasConfig/camera/vidioCodingFormat").getText());//视频编码模式
        camera.setVidioCodingFormatDesc(((Element)document.selectSingleNode("//videoMonitor/camerasConfig/camera/vidioCodingFormat")).attributeValue("desc"));//获取视频编码模式描述
        camera.setVidioResolution(document.selectSingleNode("//videoMonitor/camerasConfig/camera/vidioResolution").getText());//视频分辨率
        camera.setVidioResolutionDesc(((Element)document.selectSingleNode("//videoMonitor/camerasConfig/camera/vidioResolution")).attributeValue("desc"));//获取视频分辨率描述
        camera.setVidioFrameRate(document.selectSingleNode("//videoMonitor/camerasConfig/camera/vidioFrameRate").getText());//视频帧率
        camera.setVidioFrameRateDesc(((Element)document.selectSingleNode("//videoMonitor/camerasConfig/camera/vidioResolution")).attributeValue("desc"));//获取视频帧率描述
        camera.setVidioStreamControl(document.selectSingleNode("//videoMonitor/camerasConfig/camera/vidioStreamControl").getText());//码流控制
        camera.setVidioStreamControlDesc(((Element)document.selectSingleNode("//videoMonitor/camerasConfig/camera/vidioStreamControl")).attributeValue("desc"));//获取码流控制描述
        camera.setVidioStreamSpeed(document.selectSingleNode("//videoMonitor/camerasConfig/camera/vidioStreamSpeed").getText());//码流kb/s
        camera.setVidioStreamSpeedDesc(((Element)document.selectSingleNode("//videoMonitor/camerasConfig/camera/vidioStreamSpeed")).attributeValue("desc"));//获取码流kb/s描述
        camera.setPictureResolution(document.selectSingleNode("//videoMonitor/camerasConfig/camera/pictureResolution").getText());//图片分辨率
        camera.setPictureResolutionDesc(((Element)document.selectSingleNode("//videoMonitor/camerasConfig/camera/pictureResolution")).attributeValue("desc"));//获取图片分辨率描述
        camera.setSampleInterval(document.selectSingleNode("//videoMonitor/camerasConfig/camera/sampleInterval").getText());//采集间隔分钟
        camera.setSampleIntervalDesc(((Element)document.selectSingleNode("//videoMonitor/camerasConfig/camera/sampleInterval")).attributeValue("desc"));//获取采集间隔分钟描述

        //获取第3栏信息:网络设置
        NetConfig netConfig = new NetConfig();
        NetConfig.WiredNetwork wiredNetwork = new NetConfig.WiredNetwork();

        netConfig.setNetConfigDesc(((Element)document.selectSingleNode("//videoMonitor/netConfig/wiredNetwork")).attributeValue("desc"));//获取网络设置描述
        netConfig.setSim1Desc(((Element)document.selectSingleNode("//videoMonitor/netConfig/sim1")).attributeValue("desc"));//获取SIM1卡网络设置描述
        netConfig.setSim2Desc(((Element)document.selectSingleNode("//videoMonitor/netConfig/sim2")).attributeValue("desc"));//获取SIM2卡网络设置描述
        wiredNetwork.setIp(document.selectSingleNode("//videoMonitor/netConfig/wiredNetwork/ip").getText());//有线网络ip地址
        wiredNetwork.setIpDesc(((Element)document.selectSingleNode("//videoMonitor/netConfig/wiredNetwork/ip")).attributeValue("desc"));//获取有线网络ip地址设置描述
        wiredNetwork.setNetmask(document.selectSingleNode("//videoMonitor/netConfig/wiredNetwork/netmask").getText());//有线网络子网掩码
        wiredNetwork.setNetmaskDesc(((Element)document.selectSingleNode("//videoMonitor/netConfig/wiredNetwork/netmask")).attributeValue("desc"));//获取有线网络子网掩码设置描述

        NetConfig.Sim1 sim1 = new NetConfig.Sim1();
        sim1.setMainstationIp(document.selectSingleNode("//videoMonitor/netConfig/sim1/mainstationIp").getText());//sim1卡设置主站IP地址
        sim1.setMainstationIpDesc(((Element)document.selectSingleNode("//videoMonitor/netConfig/sim1/mainstationIp")).attributeValue("desc"));//获取sim1卡主站IP地址设置描述
        sim1.setMainstationPort(document.selectSingleNode("//videoMonitor/netConfig/sim1/mainstationPort").getText());//sim1卡设置主站端口号
        sim1.setMainstationPortDesc(((Element)document.selectSingleNode("//videoMonitor/netConfig/sim1/mainstationPort")).attributeValue("desc"));//获取sim1卡主站端口号设置描述
        sim1.setProtocol(document.selectSingleNode("//videoMonitor/netConfig/sim1/protocol").getText());//sim1卡设置通讯协议
        sim1.setProtocolDesc(((Element)document.selectSingleNode("//videoMonitor/netConfig/sim1/protocol")).attributeValue("desc"));//获取sim1卡通讯协议设置描述
        sim1.setNetMode(document.selectSingleNode("//videoMonitor/netConfig/sim1/netMode").getText());//sim1卡设置网络模式
        sim1.setNetModeDesc(((Element)document.selectSingleNode("//videoMonitor/netConfig/sim1/netMode")).attributeValue("desc"));//获取sim1卡网络模式设置描述

        NetConfig.Sim2 sim2 = new NetConfig.Sim2();
        sim2.setMainstationIp(document.selectSingleNode("//videoMonitor/netConfig/sim2/mainstationIp").getText());//sim2卡设置主站IP地址
        sim2.setMainstationIpDesc(((Element)document.selectSingleNode("//videoMonitor/netConfig/sim2/mainstationIp")).attributeValue("desc"));//获取sim2卡主站IP地址设置描述
        sim2.setMainstationPort(document.selectSingleNode("//videoMonitor/netConfig/sim2/mainstationPort").getText());//sim2卡设置主站端口号
        sim2.setMainstationPortDesc(((Element)document.selectSingleNode("//videoMonitor/netConfig/sim2/mainstationPort")).attributeValue("desc"));//获取sim2卡主站端口号设置描述
        sim2.setProtocol(document.selectSingleNode("//videoMonitor/netConfig/sim2/protocol").getText());//sim2卡设置通讯协议
        sim2.setProtocolDesc(((Element)document.selectSingleNode("//videoMonitor/netConfig/sim2/protocol")).attributeValue("desc"));//获取sim2卡通讯协议设置描述
        sim2.setNetMode(document.selectSingleNode("//videoMonitor/netConfig/sim2/netMode").getText());//sim2卡设置网络模式
        sim2.setNetModeDesc(((Element)document.selectSingleNode("//videoMonitor/netConfig/sim2/netMode")).attributeValue("desc"));//获取sim2卡网络模式设置描述

        //获取第4栏信息:存储管理
        StorageConfig storageConfig = new StorageConfig();
        storageConfig.setStorageConfigDesc(((Element)document.selectSingleNode("//videoMonitor/storageConfig")).attributeValue("desc"));//获取存储管理描述
        storageConfig.setShortVideoDuration(document.selectSingleNode("//videoMonitor/storageConfig/shortVideoDuration").getText());//短视频时长
        storageConfig.setShortVideoDurationDesc(((Element)document.selectSingleNode("//videoMonitor/storageConfig/shortVideoDuration")).attributeValue("desc"));//短视频时长描述
        storageConfig.setVideoRecording(document.selectSingleNode("//videoMonitor/storageConfig/videoRecording").getText());//是否开启录像
        storageConfig.setVideoRecordingDesc(((Element)document.selectSingleNode("//videoMonitor/storageConfig/videoRecording")).attributeValue("desc"));//是否开启录像描述
        storageConfig.setPictureStorage(document.selectSingleNode("//videoMonitor/storageConfig/pictureStorage").getText());//照片内部存储介质
        storageConfig.setPictureStorageDesc(((Element)document.selectSingleNode("//videoMonitor/storageConfig/pictureStorage")).attributeValue("desc"));//照片内部存储介质描述
        storageConfig.setVideoStorage(document.selectSingleNode("//videoMonitor/storageConfig/videoStorage").getText());//视频内部存储介质
        storageConfig.setVideoStorageDesc(((Element)document.selectSingleNode("//videoMonitor/storageConfig/videoStorage")).attributeValue("desc"));//视频内部存储介质描述

        //获取第5栏信息:系统信息
        SystemMsg systemMsg = new SystemMsg();
        systemMsg.setSystemMsgDesc((((Element)(document.selectSingleNode("//videoMonitor/systemMsg"))).attributeValue("desc")));//系统信息描述
        systemMsg.setSinglechipVersion(document.selectSingleNode("//videoMonitor/systemMsg/singlechipVersion").getText());//单片机版本号
        systemMsg.setSinglechipVersionDesc((((Element)(document.selectSingleNode("//videoMonitor/systemMsg/singlechipVersion"))).attributeValue("desc")));//获取单片机版本号
        systemMsg.setSoftwareVersion(document.selectSingleNode("//videoMonitor/systemMsg/softwareVersion").getText());//软件版本号
        systemMsg.setSoftwareVersionDesc((((Element)(document.selectSingleNode("//videoMonitor/systemMsg/softwareVersion"))).attributeValue("desc")));//获取软件版本号信息描述
        systemMsg.setSystemVersion(document.selectSingleNode("//videoMonitor/systemMsg/systemVersion").getText());//系统版本号
        systemMsg.setSystemVersionDesc((((Element)(document.selectSingleNode("//videoMonitor/systemMsg/systemVersion"))).attributeValue("desc")));//获取系统版本号信息描述
        //获取第6栏信息:系统管理
        SystemConfig systemConfig = new SystemConfig();
        systemConfig.setSystemConfigDesc(((Element)(document.selectSingleNode("//videoMonitor/systemConfig"))).attributeValue("desc"));//获取系统管理描述
        systemConfig.setRestore(document.selectSingleNode("//videoMonitor/systemConfig/restore").getText());//设备恢复出厂
        systemConfig.setRestoreDesc(((Element)(document.selectSingleNode("//videoMonitor/systemConfig/restore"))).attributeValue("desc"));//设备恢复出厂描述


    }

    //获取前端提交数据
    public Map<Object, Object> GetXmlParas(){
        Map<Object,Object> paras = new HashMap<>();
        //获取第1栏信息:设备设置
        String id = getPara("id");
        String cameraNameWatermark = getPara("cameraNameWatermark");
        String deviceInfoWatermark = getPara("deviceInfoWatermark");

        //获取第2栏信息:摄像头设置
        String name = getPara("name");
        String vidioCodingFormat = getPara("vidioCodingFormat");
        String vidioResolution = getPara("vidioResolution");
        String vidioFrameRate = getPara("vidioFrameRate");
        String vidioStreamControl = getPara("vidioStreamControl");
        String vidioStreamSpeed = getPara("vidioStreamSpeed");
        String pictureResolution = getPara("pictureResolution");
        String sampleInterval = getPara("sampleInterval");

        //获取第3栏信息:网络设置
        String ip = getPara("ip");
        String netmask = getPara("netmask");
        String mainstationIp = getPara("mainstationIp");
        String mainstationPort = getPara("mainstationPort");
        String protocol = getPara("protocol");
        String netMode = getPara("netMode");

        //获取第4栏信息:存储管理
        String shortVideoDuration = getPara("shortVideoDuration");
        String videoRecording = getPara("videoRecording");
        String pictureStorage = getPara("pictureStorage");
        String videoStorage = getPara("videoStorage");

        //获取第5栏信息:系统信息
        String singlechipVersion = getPara("singlechipVersion");
        String softwareVersion = getPara("softwareVersion");
        String systemVersion = getPara("systemVersion");

        //获取第6栏信息:系统管理
        String restore = getPara("restore");

        paras.put("id",id);
        paras.put("cameraNameWatermark",cameraNameWatermark);
        paras.put("deviceInfoWatermark",deviceInfoWatermark);
        paras.put("name",name);
        paras.put("vidioCodingFormat",vidioCodingFormat);
        paras.put("vidioResolution",vidioResolution);
        paras.put("vidioFrameRate",vidioFrameRate);
        paras.put("vidioStreamControl",vidioStreamControl);
        paras.put("vidioStreamSpeed",vidioStreamSpeed);
        paras.put("pictureResolution",pictureResolution);
        paras.put("sampleInterval",sampleInterval);
        paras.put("ip",ip);
        paras.put("netmask",netmask);
        paras.put("mainstationIp",mainstationIp);
        paras.put("mainstationPort",mainstationPort);
        paras.put("protocol",protocol);
        paras.put("netMode",netMode);
        paras.put("shortVideoDuration",shortVideoDuration);
        paras.put("videoRecording",videoRecording);
        paras.put("pictureStorage",pictureStorage);
        paras.put("videoStorage",videoStorage);
        paras.put("singlechipVersion",singlechipVersion);
        paras.put("softwareVersion",softwareVersion);
        paras.put("systemVersion",systemVersion);
        paras.put("restore",restore);

        return paras;

    }

    //写入xml文件信息
    public void WriteParameter(Map<Object, Object> paras) throws IOException {

        //写入系统信息
        Node singlechipVersion = document.selectSingleNode("//videoMonitor/systemMsg/singlechipVersion");//单片机版本号
        Node softwareVersion = document.selectSingleNode("//videoMonitor/systemMsg/softwareVersion");//软件版本号
        Node systemVersion = document.selectSingleNode("//videoMonitor/systemMsg/systemVersion");//系统版本号
        for (Object para : paras.keySet()) {
            if (singlechipVersion.equals(para)){
                singlechipVersion.setText((String) paras.get(para));
            }
            if (softwareVersion.equals(para)){
                softwareVersion.setText((String) paras.get(para));
            }
            if (systemVersion.equals(para)){
                systemVersion.setText((String) paras.get(para));
            }
        }

        FileWriter out = new FileWriter("E:\\ready\\xml\\videoMonitor1.1.0.xml");
        document.write(out);
        out.close();
    }

    public static void main(String[] args) throws Exception {
        XmlParse xmlParse = new XmlParse();
        xmlParse.GetParameter();

    }

}
